package com.aj.activity;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BaseActivity extends Activity {
	protected RelativeLayout actionbar;
	protected TextView back_button;
	protected TextView title;
	protected ImageView rightbutton;
	protected LinearLayout body;
	protected LinearLayout paper_all;

	protected LinearLayout menu_leanlayout;

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(initContentView(layoutResID));
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(initContentView(view));
	}

	@Override
	public void setContentView(View view,
			android.view.ViewGroup.LayoutParams params) {
		super.setContentView(initContentView(view, params));
	}

	protected void setActionBarTitle(String titleString) {
		if (title != null) {
			title.setText(titleString);
		}
	}

	protected void setActionBarTitle(int titleStringResID) {
		if (title != null) {
			title.setText(getString(titleStringResID));
		}
	}

	protected void setActionBarBackButtonClickEvent() {
		if (back_button != null) {
			back_button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}

	protected void setActionBarBackButtonClickEvent(OnClickListener listener) {
		if (back_button != null) {
			back_button.setOnClickListener(listener);
		}
	}

	protected void setActionBarRightButtonClickEvent(OnClickListener listener) {
		if (rightbutton != null) {
			rightbutton.setOnClickListener(listener);
		}
	}

	protected void setActionBarBackButtonText(String textString) {
		if (back_button != null) {
			back_button.setText(textString);
		}
	}

	protected void setActionBarBackButtonText(int textStringResID) {
		if (back_button != null) {
			back_button.setText(getString(textStringResID));
		}
	}

	protected void setActionBarRightButton() {
		if (rightbutton != null) {
			rightbutton.setVisibility(View.VISIBLE);
		}
	}

	private View initContentView(int layoutResID) {
		View bodyView = LayoutInflater.from(this).inflate(
				R.layout.activity_base, null);
		View contentView = LayoutInflater.from(this).inflate(layoutResID, null);
		initView(bodyView);
		body.addView(contentView, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return bodyView;
	}

	private View initContentView(View view,
			android.view.ViewGroup.LayoutParams params) {
		View bodyView = LayoutInflater.from(this).inflate(
				R.layout.activity_base, null);
		initView(bodyView);
		body.addView(view, params);
		return bodyView;
	}

	private View initContentView(View view) {
		View bodyView = LayoutInflater.from(this).inflate(
				R.layout.activity_base, null);
		initView(bodyView);
		body.addView(view, new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return bodyView;
	}

	private void initView(View bodyView) {
		actionbar = (RelativeLayout) bodyView.findViewById(R.id.actionbar);
		back_button = (TextView) bodyView.findViewById(R.id.back_button);
		title = (TextView) bodyView.findViewById(R.id.title);
		rightbutton = (ImageView) bodyView.findViewById(R.id.rightbutton);
		body = (LinearLayout) bodyView.findViewById(R.id.body);
		menu_leanlayout = (LinearLayout) bodyView
				.findViewById(R.id.menu_leanlayout);
		paper_all = (LinearLayout) bodyView
				.findViewById(R.id.paper_all);
		setActionBarBackButtonClickEvent();
		paper_all.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
	}

	protected void setMenuLayout(View view) {
		if (view != null && menu_leanlayout != null) {
			menu_leanlayout.addView(view, new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		}
	}

	protected void openPanel() {
		Animation anim = AnimationUtils.loadAnimation(this,
				R.anim.slide_bottom_in);
		menu_leanlayout.setVisibility(View.VISIBLE);
		paper_all.setVisibility(View.VISIBLE);
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				menu_leanlayout.clearAnimation();
			}
		});
		menu_leanlayout.startAnimation(anim);
	}

	protected void closePanel() {
		Animation anim = AnimationUtils.loadAnimation(this,
				R.anim.slide_bottom_out);
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				menu_leanlayout.setVisibility(View.GONE);
				paper_all.setVisibility(View.GONE);
				menu_leanlayout.clearAnimation();
			}
		});
		menu_leanlayout.startAnimation(anim);
	}
}
